#include <bits/stdc++.h>
#define int long long
using namespace std;
// char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    return x*f;
}
const int mod=147744151,N=1000,M=168;
int n,m,p=998244353,q,ans;
int pri[N],cnt,vis[N],bj;
#define pow Pow
int pow(int x,int base,int mod=mod){
    int ans=1;
    while(base){
        if(base&1) ans=ans*x%mod;
        x=x*x%mod;
        base>>=1;
    }
    return ans;
}
int gcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
bool check(int n){
    if(n<3||n&1^1) return n==2;
    int u=n-1,t=0,test[]={2,325,9375,28178,450775,9780504,1795265022};
    while(u&1^1) t++,u>>=1;
    for(int i:test){
        int c=pow(i,u,n);
        if(c==1||c==n-1||c==0) continue;
        for(int j=1;j<=t;j++){
            c=c*c%n;
            if(c==n-1&&j!=t){
                c=1;
                break;
            }
            if(c==1) return 0;
        }
        if(c!=1) return 0;
    }
    return 1;
}
int pr(int n){
    if(n==1) return 1;
    if(n==4) return 2;
    if(check(n)) return n;
    while(1){
        int c=rand()%(n-1)+1;
        auto f=[=](int x){return(x*x+c)%n;};
        int t=0,r=0,p=1,q;
        do{
            for(int i=0;i<=128;i++){
                t=f(t),r=f(f(r));
                if(t==r||(q=p*abs(t-r)%n)==0) break;
                p=q;
            }
            int d=gcd(p,n);
            if(d>1) return d;
        }while(t!=r);
    }
}
struct name{
    int v[M+1]; 
    unordered_map<int,int> t;
}a[N+5][N+5];
name operator *(name a,name b){
    for(int i=1;i<=M;i++) a.v[i]+=b.v[i];
    for(auto x:b.t) a.t[x.first]+=x.second;
    return a;
}
name operator /(name a,name b){
    for(int i=1;i<=M;i++) a.v[i]-=b.v[i];
    for(auto x:b.t) a.t[x.first]-=x.second;
    return a;
}
name chann(int a){
    name c;
    for(int i=1;i<=M;i++){
        c.v[i]=0;
        while(a%pri[i]==0){
            c.v[i]++;
            a/=pri[i];
        }
    }
    if(a==1) return c;
    int x=pr(a);
    c.t[x]++;
    if(a!=x) c.t[a/x]++;
    return c;
}
int chani(name a,int mod=p){
    int ans=1;
    for(int i=1;i<=M;i++){
        if(a.v[i]){
            ans=ans*pow(pri[i],a.v[i],mod)%mod;
        }
    }
    for(auto x:a.t){
        ans=ans*pow(x.first,x.second,mod)%mod;
    }
    return ans;
}
void out(name a){
    for(int i=1;i<=M;i++){
        if(a.v[i]) cout<<pri[i]<<" "<<a.v[i]<<"\n";
    }
    cout<<"\n";
}
signed main(){
    freopen("tsuki.in","r",stdin);
    freopen("tsuki.out","w",stdout);
    for(int i=2;i<=N;i++){
        if(vis[i]) continue;
        pri[++cnt]=i;
        for(int j=i+i;j<=N;j+=i) vis[j]=1;
    }
    n=read(),m=read(),p=read();
    for(int i=1;i<=n;i++)
        for(int j=1,x;j<=m;j++){
            x=read();
            a[i][j]=chann(x);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            a[i][j]=a[i][j]*a[i-1][j]*a[i][j-1]/a[i-1][j-1];
        }
    q=read();
    for(int i=1;i<=q;i++){
        int x1=read(),y1=read(),x2=read(),y2=read();
        name c=a[x2][y2]*a[x1-1][y1-1]/a[x1-1][y2]/a[x2][y1-1];
        int ansi=chani(c,p);
        ans=(ans+i^ansi)%mod;
    }
    cout<<ans;
    return 0;
}